/**
 * Copyright (C) 2011-2012 Luca Li Greci
 *
 * This file is part of pojo-creator
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.pojocreator.provider.string;

import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.RandomStringUtils;

import com.pojocreator.provider.DataProvider;

/**
 * This class implements {@link DataProvider} to provide a random alphabetic char.
 * <p>
 * The set of character considered is between ('z'+1 - 'a') + ('Z'+1 - 'A'), in total are 52 chars. 
 * 
 * @author Luca Li Greci
 */
public class RandomChar implements DataProvider < Character > {
    private static final int NUMBER_OF_LETTERS = 1;
    private static final int MAX_NUMBER_OF_ELEMENTS = ('z'+1 - 'a') + ('Z'+1 - 'A');
    private Set < Character > unique;
    
    /**
     * Create an instance of RandomChar.
     */
    public RandomChar() {
        unique = new HashSet < Character >();
    }
    
    @Override
    public Character data() {
        verifySizeOfAndClearCharacterSet();
        return extractUniqueCharacter();
    }


    /**
     * Return the type of element managed.
     * 
     * @return The type of element managed is <code>Character.class</code> 
     */
    @Override
    public Class < Character > type() {
        return Character.class;
    }
    
    /**
     * The number of elements returned is 1
     * 
     * @return number of elements returned is 1
     */
    @Override
    public int size() {
        return 1;
    }
    

    /**
     * Extract a {@link Character} not yet extracted, so that can be considered
     * unique among all possible chars {@link RandomChar#MAX_NUMBER_OF_ELEMENTS}
     * 
     * @return a unique {@link Character} not yet extracted.
     */
    private Character extractUniqueCharacter() {
        Character uniqueCharacter = pickupCharacter();
        while (unique.contains(uniqueCharacter)) {
            uniqueCharacter = pickupCharacter();
        }
        unique.add(uniqueCharacter);
        return uniqueCharacter;
    }
    
    /**
     * Return a {@link Character} from the 52 available 
     * 
     * @return a random {@link Character} 
     */
    private static Character pickupCharacter() {
        return Character.valueOf(RandomStringUtils.randomAlphabetic(NUMBER_OF_LETTERS).charAt(0));
    }
    
    /**
     * Verify whether or not the max number of allowed elements have been reached. If so, the underly set
     * is cleared down for a new cycle of extraction
     */
    private void verifySizeOfAndClearCharacterSet() {
        if (unique.size() == MAX_NUMBER_OF_ELEMENTS) {
            unique.clear();
        }
    }

}
